package org.dizena.config;

import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.jdbc.core.JdbcTemplate;

import javax.sql.DataSource;

@Configuration
public class DbConfig {
    @Bean
    @Primary
    @ConfigurationProperties("spring.datasource")
    public DataSourceProperties dataSourceProperties() {
        return new DataSourceProperties();
    }

    @Bean
    @Primary
    public DataSource dataSource(@Qualifier("dataSourceProperties") DataSourceProperties dsp) {
        HikariConfig hc = new HikariConfig();
        hc.setDriverClassName(dsp.getDriverClassName());
        hc.setJdbcUrl(dsp.getUrl());
        hc.setUsername(dsp.getUsername());
        hc.setPassword(dsp.getPassword());
        hc.setMaximumPoolSize(30);
        HikariDataSource hds = new HikariDataSource(hc);
        return hds;
    }

    @Bean
    @Primary
    public JdbcTemplate jdbcTemplate(@Qualifier("dataSource") DataSource ds) {
        return new JdbcTemplate(ds);
    }

    //db2
    @Bean
    @ConfigurationProperties("center.datasource")
    public DataSourceProperties dataSourceProperties2() {
        return new DataSourceProperties();
    }

    @Bean
    public DataSource dataSource2(@Qualifier("dataSourceProperties2") DataSourceProperties dsp) {
        return dsp.initializeDataSourceBuilder().build();
    }

    @Bean
    public JdbcTemplate jdbcTemplate2(@Qualifier("dataSource2") DataSource ds) {
        return new JdbcTemplate(ds);
    }


}
